
var affiliates =  {
	sn: {
		url: "http://www.sachsens-stube.de/",
		// TODO: use https://play.google.com/store/apps/details?id=de.slothsoft.stube.sn
		eventName: "Sachsen"
	},
//	by: {
//		url: "http://www.bazi-stüble.de/",
//		// TODO: use https://play.google.com/store/apps/details?id=de.slothsoft.stube.by
//		eventName: "Bayern"
//	}
};


function getFaviconUrl(id) {
	if (!isAffiliate(id))
		return "gfx/x.ico";
	return id.toLowerCase() + "/favicon.ico";
}

function getWebsite(id) {
	if (!isAffiliate(id))
		return "http://slothsoft.de";
	return affiliates[id].url;
}

function isAffiliate(id) {
	return affiliates.hasOwnProperty(id);
}

function findFederalStateIdByName(name) {
	for (id in affiliates) {
		if (affiliates[id].eventName == name)
			return id;
	}
	return "--";
}

function findFederalStateId(result) {
	var stateId = findShortName(result, 'administrative_area_level_1');
	return (stateId) ? stateId.toLowerCase() : "--";
}

function findFederalState(result) {
	var state = findLongName(result, 'administrative_area_level_1');
	return (state) ? state : "undefined";
}

function findLongName(result, type) {
	var addressComponents = result['address_components'];
	for (var i = 0; i < addressComponents.length; i++) {
		if (isType(addressComponents[i], type))
			return addressComponents[i]['long_name'];
	}
	return null;
}

function findShortName(result, type) {
	var addressComponents = result['address_components'];
	for (var i = 0; i < addressComponents.length; i++) {
		if (isType(addressComponents[i], type))
			return addressComponents[i]['short_name'];
	}
	return null;
}

function isType(addressComponent, type) {
	var types = addressComponent['types'];
	for (var i = 0; i < types.length; i++) {
		if (types[i] == type)
			return true;
	}
	return false;
}